package train.DAY625;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",9090);
        InputStream inputStream = socket.getInputStream();
//        byte[] a=new byte[12];
//        inputStream.read(a);
//        System.out.println(new String(a));

        new Thread() {
            @Override
            public void run() {
                OutputStream os = null;


                while (true) {
                    //发信息
                    Scanner scanner = new Scanner(System.in);
                    String input = scanner.nextLine();//block

                    try {

                        os = socket.getOutputStream();
                        os.write(input.getBytes());
                        os.flush();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    if (input.equals("quit")) {
                        break;
                    }
                }
            }
        }.start();

String msg = null;
        //主线程收
        byte[] data = new byte[100];
        while (true) {
            try {
              int len=  inputStream.read(data);//block
                 msg=new String(data,0,len);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(socket.getRemoteSocketAddress()+ ":"+msg);
        }

/*
//收信息
                                InputStream inputStream = accept.getInputStream();
                                byte[] data = new byte[100];
                                int len = inputStream.read(data);//block
                                String msg = new String(data, 0, len);

                                System.out.println(accept.getRemoteSocketAddress() + "：" +msg);
 */

    }
}
